<?php
    /**
    * List of notes belongs to meeting
    */
    if (!defined('EXPONENT')) exit('');
    function _manage_meetings_return_empty()
    {
        $ret_object = null;
        $ret_object->total = 0;
        $ret_object->rows=array(); 
        echo json_encode($ret_object);
        return;
    }

    if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate","meeting","meeting_delete","meeting_assistant"),exponent_core_makeLocation("profilemodule","_syscore","")))
    {
        global $user;
        $note=null;
        if(isset($_REQUEST['_new'])&&exponent_permissions_check(array("administrate","meeting","meeting_assistant"),exponent_core_makeLocation("profilemodule","_syscore","")))
        {
            if($_REQUEST['edit']>0)
            {
                //Check permiss for meeting admin or meeting assitant can edit note
                $note=$db->selectObject("profilemodule_meeting_notes","id=".intval($_REQUEST['edit']));
                $note->last_edit=time();
                $note->subject=$_REQUEST['subject'];
                $note->content=$_REQUEST['content'];

                $db->updateObject($note,'profilemodule_meeting_notes');
                
                profilemodule::_meeting_history_add($note->meeting_id,"Note id = {$note->id}; Note subject = {$note->subject} ; Note content = {$note->content}", "Update note");
                
            }
            else
            {
                if (isset($_REQUEST['id']))
                    $id = intval($_REQUEST['id']);
                $note->meeting_id=$id;
                $note->post_by=$user->username;
                $note->post_date=time();
                $note->subject=$_REQUEST['subject'];
                $note->content=$_REQUEST['content'];

                $db->insertObject($note,"profilemodule_meeting_notes");
                profilemodule::_meeting_history_add($id,"Note id = {$note->id}; Note subject = {$note->subject} ; Note content = {$note->content}", "Insert note");
            }   



        }
        else
        {
            $id=0;
            if (isset($_REQUEST['id']))
                $id = intval($_REQUEST['id']);
            if ($id ==0)
                _manage_meetings_return_empty();
            else
            {
                // select user
                $sql_where="meeting_id = {$id}";
                if (isset($_REQUEST['search']))
                {
                    $search = $_REQUEST['search'];
                    $sql_where.=" AND subject LIKE '%{$search}%' OR content LIKE '%{$search}%'";
                }

                $page=1;
                if (isset($_REQUEST['page']))
                    $page=intval($_REQUEST['page']);
                $pageSize=10;

                if (isset($_REQUEST['rows']))
                    $pageSize=intval($_REQUEST['rows']);
                $start=($page-1)*$pageSize;

                // sort
                $sort="";
                if (isset($_REQUEST['sort'])){
                    $sort = " ORDER BY {$_REQUEST['sort']} {$_REQUEST['order']}";
                }

                $cuser = $db->selectObjects("profilemodule_meeting_notes", "{$sql_where} {$sort} LIMIT {$start},{$pageSize}");

                foreach ($cuser as $k=>$v)
                {
                    $v->post_date_text = date('d/m/Y H:i:s',$v->post_date);                
                }
                $ret_object = null;
                $ret_object->total = $db->countObjects('profilemodule_meeting_notes',"{$sql_where}");
                $ret_object->rows = $cuser; 
                echo json_encode($ret_object);

            }
        }
    }

?>